﻿package com.singlenotion.form 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author Adam Jowett (Single Notion)
	 */
	public class FormError extends MovieClip
	{
		private var _error:String;
		private var _fontName:String;
		private var _formField:Array;
		private var _align:String;
		private var _fontSize:int;
		private var _errorColour:int;
		private var _indicator:Class;
		private var _txtField:TextField;
		private var _txtFormat:TextFormat;
		private var _alwaysShow:Boolean;
		
		public function FormError(	$error:String, $fontName:String, $formField:Array = null, $align:String = "errorRight", 
									$fontSize:int = 16, $errorColour:int = 0x990000, $indicator:Class = null, $alwaysShow:Boolean = false)
		{
			_error = $error;
			_fontName = $fontName;
			_align = $align;
			_formField = $formField;
			_fontSize = $fontSize;
			_errorColour = $errorColour;
			_indicator = $indicator;
			_alwaysShow = $alwaysShow;
			
			init();
		}
		
		public function init():void
		{
			var offset:Number = 0;
			
			_txtField = new TextField();
			_txtFormat = new TextFormat();
			
			_txtFormat.color = _errorColour;
			_txtFormat.font = _fontName;
			_txtFormat.size = _fontSize;
			
			_txtField.defaultTextFormat = _txtFormat;
			_txtField.embedFonts = true;
			_txtField.autoSize = TextFieldAutoSize.LEFT;
			_txtField.type = TextFieldType.DYNAMIC;
			_txtField.textColor = _errorColour;
			_txtField.border = false;
			_txtField.background = false;
			_txtField.antiAliasType = AntiAliasType.ADVANCED;
			
			_txtField.text = _error;
			
			if (_indicator != null) {
				var errorIndicator:MovieClip = new _indicator();
				addChild(errorIndicator);
				offset = errorIndicator.width;
			}
			
			_txtField.x = offset + 5;
			
			if (_formField != null) {
				var l:int = _formField.length;
				x = _formField[l - 1].x + _formField[l - 1].width + 5;
				y = _formField[l - 1].y;
				for each (var field:FormField in _formField) {
					if (_alwaysShow) {
						field.addEventListener(FormFieldEvent.VALID_ERROR, validListener, false, 0, true);
						field.addEventListener(FormFieldEvent.VALID_CONFIRM, validListener, false, 0, true);
					} else {
						field.addEventListener(FormFieldEvent.MANUAL_VALIDATE, validListener, false, 0, true);
					}
				}
			}
			
			addChild(_txtField);
			
			if (!_alwaysShow) hide();
		}
		
		private function validListener($e:Event):void 
		{
			switch ($e.type) {
				case FormFieldEvent.VALID_ERROR:
				show();
				break;
				default:
				for each (var field:FormField in _formField) {
					if (!field.valid) {
						show();
						return;
					}
				}
				hide();
				break;
			}
		}
		
		public function updateError($errorText:String)
		{
			_txtField.text = $errorText;
		}
		
		public function show($e:Event = null):void
		{
			TweenLite.to(this, 0.3, { autoAlpha:1 } );
		}
		
		public function hide($e:Event = null):void
		{
			TweenLite.to(this, 0.3, { autoAlpha:0 } );
		}
	}
}